package com.solcoo.merchant.base;

import java.util.concurrent.atomic.AtomicBoolean;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;

import com.solcoo.common.App;
import com.solcoo.merchant.ApplicationExt;
import com.solcoo.merchant.R;
import com.solcoo.merchant.base.i.BaseViewWrapper;
import com.solcoo.merchant.base.task.BindContextAsyncTask;
import com.solcoo.merchant.ui.login.LoginActivity;
import com.solcoo.merchant.util.DialogUtil;
import com.solcoo.merchant.util.ViewUtil;

public class BaseActivity extends Activity implements BaseViewWrapper {

	protected static final String TAG = "Activity";

	protected ProgressDialog progressDialog;
	private ViewStub noDataStub;
	private View mLinearNoData;

	private static AtomicBoolean init = new AtomicBoolean(false); // 是否已经初始化过某些必要服务，比如UserTrack、后台Pull进程等

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.v(TAG, "onCreate activity:" + toString());
		progressDialog = ViewUtil.createProgressDialog(this,
				R.string.do_loading);
		App.onCreateActivity(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		// noDataStub = (ViewStub) findViewById(R.id.view_stub_of_no_data);
		if (!(this instanceof LoginActivity)) {
			ViewUtil.setGoBackClickListener(findViewById(R.id.btn_go_back));
		}
		ApplicationExt.getInstance().setCurrentContext(this);
	}

	@Override
	protected void onDestroy() {
		BindContextAsyncTask.clearTask(this);
		App.onDestoryActivity(this);
		super.onDestroy();
	}

	@Override
	public View showNotData(int... exceptIds) {
		if (noDataStub == null && mLinearNoData == null) {
			Log.w(TAG, getClass() + " do not found mLinearNoData view");
			return null;
		}
		if (mLinearNoData == null) {
			mLinearNoData = inflateNoDataView();
		}
		ViewUtil.showNotData(mLinearNoData, this, exceptIds);
		return mLinearNoData;
	}

	@Override
	public void dismissNotData() {
		if (mLinearNoData != null) {
			mLinearNoData.setVisibility(View.GONE);
		}
	}

	protected View inflateNoDataView() {
		if (mLinearNoData == null) {
			mLinearNoData = noDataStub.inflate();
		}
		return mLinearNoData;
	}

	public void dismissProgressDialog() {
		DialogUtil.dimissDialog(progressDialog);
	}

	public void setProgressDialogMessage(String msg) {
		progressDialog.setMessage(msg);
	}

	@Override
	public void showProgressDialog() {
		if (progressDialog != null)
			progressDialog.show();
	}
}
